package collection;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class HashMapTest {


    public static void main(String[] args) {


        HashMap<String, String> map = new HashMap();
        map.put("1","1");
        map.put("2","2");
        map.put("44","44");
        map.put("3","3");
//        for (Map.Entry<String, String> entry : map.entrySet()){
//            System.out.println(entry.getKey() +": "+entry.getValue());
//        }



        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put("1","1");
        linkedHashMap.put("2","2");
        linkedHashMap.put("3","3");
        linkedHashMap.put("44","44");
//        for (Map.Entry<String, String> entry : linkedHashMap.entrySet()){
//            System.out.println(entry.getKey() +": "+entry.getValue());
//        }


        // accessOrder 决定了按照什么排序
        // true 则按照访问顺序   false 则按照插入顺序
        LinkedHashMap<String, String> linkedHashMap1 = new LinkedHashMap<>(1<<4,0.75f, false);
        linkedHashMap1.put("1","1");
        linkedHashMap1.put("2","2");
        linkedHashMap1.put("3","3");
        linkedHashMap1.put("44","44");
        for (Map.Entry<String, String> entry : linkedHashMap1.entrySet()){
            System.out.println(entry.getKey() +": "+entry.getValue());
        }
        System.out.println("-----------------------");


        LinkedHashMap<String, String> linkedHashMap2 = new LinkedHashMap<>(16,0.75f, true);
        linkedHashMap2.put("1","1");
        linkedHashMap2.put("2","2");
        linkedHashMap2.put("3","3");
        linkedHashMap2.put("44","44");
        // 访问过得元素会被移到链表末尾
        linkedHashMap2.get("44");
        linkedHashMap2.get("3");
        for (Map.Entry<String, String> entry : linkedHashMap2.entrySet()){
            System.out.println(entry.getKey() +": "+entry.getValue());
        }


        System.out.println("-----------------------");

        // LRU 缓存
        LruMap<String, String> lruMap = new LruMap<>(2);
        lruMap.put("1","1");
        lruMap.put("2","2");
        lruMap.put("3","3");
        lruMap.put("44","44");
        for (Map.Entry<String, String> entry : lruMap.entrySet()){
            System.out.println(entry.getKey() +": "+entry.getValue());
        }


    }



}
